package com.example.myinput;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class FileOp {
    public static boolean write(String fullName,String content,boolean append){
        try {
            File dir=new File(fullName);
            if(dir.getParentFile()!=null&&!dir.getParentFile().exists()){
                dir.getParentFile().mkdirs();
            }
            FileWriter writer=new FileWriter(dir,append);
            writer.write(content);
            writer.close();
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    public static String read(String fullName){
        try {
            File dir=new File(fullName);
            String content="";
            if(dir.exists()){
                BufferedReader reader = new BufferedReader(new FileReader(dir));
                String line;
                while ((line = reader.readLine()) != null) {
                    content+=line;
                }
                return content;
            }
        }catch (Exception e){
        }
        return "";
    }
    public static JSONObject readJson(String fullName){
        try {
            String content=read(fullName);
            if(content!=""){
                return new JSONObject(content);
            }
        }catch (Exception e){
        }
        return null;
    }
    public static boolean writeJson(String fullName,JSONObject json){
        try{
            write(fullName,json.toString(),false);
            return true;
        }catch (Exception e){

        }
        return false;
    }
}
